package client;

import model.User;
import model.UserMessage;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 管理员系统界面
 */
public class ClientManagerView {
    private ClientInitClose cic;
    private UserMessage tum;

    //构造方法，传递客户端类合管理员用户信息类到该页面
    public ClientManagerView(ClientInitClose cic, UserMessage tum) {
        this.cic=cic;
        this.tum=tum;
    }

    /**
     * 管理员系统主界面
     */
    public void ManagerMainView() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t   管理员系统");
            System.out.println("-------------------------------------------");
            System.out.println("学员管理模块：\t[1] 增加学员\t[2] 删除学员\t[3] 修改学员\t[4] 查找学员");
            System.out.println("考题管理模块：\t[5] 增加考题\t[6] 删除考题\t[7] 修改考题\t[8] 查找考题\t[9] 导入考题");
            System.out.println("             \t[0] 退出\t");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    System.out.println("正在增加学员...");addUser();break;
                case 2:
                    System.out.println("正在删除学员...");deleteUser();break;
                case 3:
                    System.out.println("正在修改学员...");upDataUser();break;
                case 4:
                    System.out.println("正在查找学员...");searchUser();break;
                case 5:
                    System.out.println("正在增加考题...");addRuestions();break;
                case 6:
                    System.out.println("正在删除考题...");deleteRuestions();break;
                case 7:
                    System.out.println("正在修改考题...");updataRuestions();break;
                case 8:
                    System.out.println("正在查找考题...");searchRuestions();break;
                case 9:System.out.println("正在导入考题...");Rueinputstions();break;
                case 0:
                   return;

            }
        }
    }

    /**
     * 导入考题
     */
    private void Rueinputstions() throws IOException {
        cic.getOos().writeUTF("导入考题");
        cic.getOos().flush();
        System.out.println("请输入考题文件txt。格式为一行题目一行答案，一行题目一行答案");
       // String path=ClientScanner.getScanner().next();
        Map<String,String> questions=new TreeMap<>();
        BufferedReader br = new BufferedReader(new FileReader("./imput.txt"));
        List<String> list=new ArrayList<>();//保存读取的每一行字符串
        String str = null;
        while ((str = br.readLine()) != null) {
            list.add(str);
        }
        int size=list.size()%2==0?list.size():list.size()-1;//定义长度
        //添加问题与答案
        for (int i = 1; i <=size ; i=i+2) {
            String ques=list.get(i-1);
            String answer=list.get(i);
            questions.put(ques,answer);
            System.out.println(ques+"---->"+answer);
        }
        cic.getOos().writeObject(questions);
        cic.getOos().flush();
        System.out.println(cic.getOis().readUTF());

    }

    /**
     * 查找考题
     */
    private void searchRuestions() throws IOException, ClassNotFoundException {
        cic.getOos().writeUTF("查找考题");
        cic.getOos().flush();

        String search=ClientScanner.getScanner().next();
        cic.getOos().writeUTF(search);
        cic.getOos().flush();

        Map<String,String> results= (Map<String, String>) cic.getOis().readObject();
        if (results.size()>0){
            System.out.println("查找结果如下");
            for (var v:results.keySet()){
                System.out.println("题目："+v+"--->答案："+results.get(v));
            }
        }else {
            System.out.println("没有找到你要的结果");
        }
    }

    /**
     * 修改考题
     */
    private void updataRuestions() throws IOException, ClassNotFoundException {
        cic.getOos().writeUTF("修改考题");
        cic.getOos().flush();
        //接收所有考题
        Map<String, String> stringStringMap= (Map<String, String>) cic.getOis().readObject();
        //在客户端展示出所有考题
        Map<Integer,String> numToQues=new TreeMap<>();//记录题号与题目对应关系
        int num=1;
        if (stringStringMap.size()>0){
        for (var v:stringStringMap.keySet()){
            System.out.println("题号"+num+"-----题目："+v+"-->答案："+stringStringMap.get(v));
            numToQues.put(num,v);
            num++;
        }}else {
            System.out.println("考题还没有哦");
        }

        System.out.println("要修改的试题题号");
        int numID= Integer.parseInt(ClientScanner.getScanner().next());
        String question=numToQues.get(numID);//获取对应的题目
        cic.getOos().writeUTF(question);
        cic.getOos().flush();


        System.out.println("修改后的新问题");
        String newquestion=ClientScanner.getScanner().next();//获取对应的题目
        cic.getOos().writeUTF(newquestion);
        cic.getOos().flush();


        System.out.println("修改后的新答案");
        String answer=ClientScanner.getScanner().next();//获取对应的题目
        cic.getOos().writeUTF(answer);
        cic.getOos().flush();

        System.out.println(cic.getOis().readUTF());
    }

    /**
     * 删除考题
     */
    private void deleteRuestions() throws IOException, ClassNotFoundException {
        //发送请求
        cic.getOos().writeUTF("删除考题");
        cic.getOos().flush();
        //接收所有考题
        Map<String, String> stringStringMap= (Map<String, String>) cic.getOis().readObject();
        //在客户端展示出所有考题
        Map<Integer,String> numToQues=new TreeMap<>();//记录题号与题目对应关系
        int num=1;
        if (stringStringMap.size()>0){
        for (var v:stringStringMap.keySet()){
            System.out.println("题号"+num+"-----题目："+v+"-->答案："+stringStringMap.get(v));
            numToQues.put(num,v);
            num++;
        }}else {
            System.out.println("考题还没有哦");
        }

        System.out.println("要删除的试题题号");
        int numID= Integer.parseInt(ClientScanner.getScanner().next());
        String question=numToQues.get(numID);//获取对应的题目
        cic.getOos().writeUTF(question);
        System.out.println("正在请求服务器");
        cic.getOos().flush();
        System.out.println(cic.getOis().readUTF());
    }

    /**
     * 增加考题
     */
    private void addRuestions() throws IOException {
        cic.getOos().writeUTF("增加考题");
        System.out.println("请输入问题");
        String question=ClientScanner.getScanner().next();
        System.out.println("请输入答案");
        String answer=ClientScanner.getScanner().next();
        System.out.println("正在请求服务器");
        cic.getOos().writeUTF(question);
        cic.getOos().flush();
        cic.getOos().writeUTF(answer);
        cic.getOos().flush();
        System.out.println(cic.getOis().readUTF());

    }

    /**
     * 根据名字查找学员
     */
    private void searchUser() throws IOException, ClassNotFoundException {
        //发送请求信息
        cic.getOos().writeUTF("查找学员");
        cic.getOos().flush();
        String name=ClientScanner.getScanner().next();
        cic.getOos().writeUTF(name);//发送数据
        cic.getOos().flush();
        System.out.println("发送"+name+"到服务器");
        List<User> users= (List<User>) cic.getOis().readObject();//接收结果
        if(users.size()>0){
            System.out.println("查找结果如下");
            for (User u:users){
                System.out.println(u.getUserName()+"--->"+u.getPassword());
            }
        }else {
            System.out.println("没有这样的学员");
        }

    }

    /**
     * 修改学员
     */
    private void upDataUser() throws IOException, ClassNotFoundException {
        //发送请求信息
        cic.getOos().writeUTF("修改学员");
        cic.getOos().flush();
        //接收所有学员信息
        List<User> users= (List<User>) cic.getOis().readObject();
        //遍历展示在客户端
        for(User u:users){
            System.out.println(u.getUserName()+"---->"+u.getPassword());
        }
        System.out.println("请输入要修改的输入学员姓名");
        String name=ClientScanner.getScanner().next();
        System.out.println("请输入修改后的密码");
        String password=ClientScanner.getScanner().next();
        User user=new User(name,password);
        cic.getOos().writeObject(user);//发送改用户名修改后的密码
        System.out.println(cic.getOis().readUTF());//打印结果
    }

    /**
     * 删除学员
     */
    private void deleteUser() throws IOException, ClassNotFoundException {
        //发送请求信息
        cic.getOos().writeUTF("删除学员");
        cic.getOos().flush();
        //接收所有学员信息
        List<User> users= (List<User>) cic.getOis().readObject();
        //遍历展示在客户端
        for(User u:users){
            System.out.println(u.getUserName()+"---->"+u.getPassword());
        }
        System.out.println("请要删除的输入学员姓名");
        String name=ClientScanner.getScanner().next();
        System.out.println("发送服务器");
        //发送该名字到服务器
        cic.getOos().writeUTF(name);
        cic.getOos().flush();
        //接收结果
        System.out.println(cic.getOis().readUTF());
    }

    /**
     * 增加学员
     */
    private void addUser() throws IOException {
        System.out.println("请设置新增的学员账户信息");
        String userName=ClientScanner.getScanner().next();
        System.out.println("请设置新增的学员初始密码");
        String password=ClientScanner.getScanner().next();
        System.out.println("发送服务器请求校验中...");
        User user=new User(userName,password);
        //发送请求信息
        cic.getOos().writeUTF("增加学员");
        //发送新增对象信息
        cic.getOos().writeObject(user);
        //接收服务器返回信息
        String check=cic.getOis().readUTF();
        System.out.println(check);//打印服务器信息
    }
}
